package Population;

import java.awt.Color;
import java.util.Random;

import javax.swing.text.Position;

import World.*;


public class Inhabitant {
	boolean isAlive;
	int x_pos;
	int y_pos;
	int hp = 10;
	int speed =1;
	int sightRange = 3;
	String type = null;
	Color color = null;
	
	public Inhabitant(int x, int y, String t, Color c)
	{
		x_pos=x;
		y_pos=y;
		isAlive = true;
		type = t;
		color = c;
	}
	
	
	public int getX()
	{
		return x_pos;
	}
	public int getY()
	{
		return y_pos;
	}
	public boolean getLife()
	{
		return isAlive;
	}
	public int hp()
	{
		return hp;
	}
	public String getType()
	{
		return type;
	}
	public Color getColor()
	{
		return color;
	}
	
	
	public void AI()
	{
		int num;
		Random rGen = new Random();
		if(CheckForFriends())
		{
			Inhabitant tempInhab = GetClosestFriend();
			if (tempInhab != null)
			{
				num = rGen.nextInt(100);
				if(num<=5)
				{
					AI_RandomMove();
				}
				MoveToward(tempInhab.getX(), tempInhab.getY());
			}
		}
		else
		{
			AI_RandomMove();
		}
	}
	
	
	public void AI_RandomMove()
	{
		boolean moved = false;
		Random rGen = new Random();
		while (!moved)
		{
			int n = rGen.nextInt(8);
			switch(n)
			{
			case 0:
				moved = true;
				break;
			case 1:
				moved = Move(x_pos+1,y_pos+1);
				break;
			case 2:
				moved = Move(x_pos+1,y_pos);
				break;
			case 3:
				moved = Move(x_pos+1,y_pos-1);
				break;
			case 4:
				moved = Move(x_pos,y_pos+1);
				break;
			case 5:
				moved = Move(x_pos,y_pos-1);
				break;
			case 6:
				moved = Move(x_pos-1,y_pos+1);
				break;
			case 7:
				moved = Move(x_pos-1,y_pos);
				break;
			case 8:
				moved = Move(x_pos-1,y_pos-1);
				break;
			default:
				break;
			}
		}
	}
	
	public boolean Move(int x, int y)
	{ 
		// check if the move is legal
		if(x<World.environGrid[0].length && y<World.environGrid.length && x>=0 && y>=0)
		{
			if(Math.abs(x-x_pos)<=speed && Math.abs(y-y_pos)<=speed && World.environGrid[y][x].isHabitable && !CollisionDetect(x,y))
			{
				x_pos = x;
				y_pos = y;
				return true;
			}
		}
		return false;
	}
	
	public boolean CollisionDetect(int x, int y)
	{
		for(int i=0; i<World.population.length; i++)
		{
			if(World.InhabitantCollision(x, y))
			{
				return true; 
			}
		}
		return false;
	}
	
	public boolean CheckForFriends()
	{
		for(int i=0; i<World.population.length; i++)
		{
			if(DistanceToFriend(World.population[i])>0 && DistanceToFriend(World.population[i])<= sightRange)
			{// friend detected
				return true;
			}
		}
		return false;
	}
	
	public boolean MoveToward(int x, int y)
	{
		if(x_pos < x && y_pos < y)
		{
			if(Move(x_pos+speed, y_pos+speed))	
				return true;
			else
				return false;
		}
		else if(x_pos < x && y_pos > y)
		{
			if(Move(x_pos+speed, y_pos-speed))
				return true;
			else
				return false;
		}
		else if(x_pos < x && y_pos == y)
		{
			if(Move(x_pos+speed, y_pos))
				return true;
			else
				return false;
		}
		else if(x_pos == x && y_pos > y)
		{
			if(Move(x_pos, y_pos-speed))
				return true;
			else
				return false;
		}
		else if(x_pos == x && y_pos < y)
		{
			if(Move(x_pos, y_pos+speed))
				return true;
			else
				return false;
		}
		else if(x_pos > x && y_pos > y)
		{
			if(Move(x_pos-speed, y_pos-speed))
				return true;
			else 
				return false;
		}
		else if(x_pos > x && y_pos == y)
		{
			if(Move(x_pos-speed, y_pos))
				return true;
			else
				return false;
		}
		else if(x_pos > x && y_pos < y)
		{
			if(Move(x_pos-speed, y_pos+speed))
				return true;
			else
				return false;
		}
		return false;
	}
	
	public Inhabitant GetClosestFriend()
	{
		double distance = sightRange+1, newDistance;
		int location = -1;
		Inhabitant tempInhab = null;
		for(int i=0; i<World.population.length; i++)
		{
			newDistance = DistanceToFriend(World.population[i]);
			if(newDistance <= sightRange && newDistance > 0)
			{
				if(newDistance < distance)
				{
					distance = newDistance;
					location = i;
				}
			}
		}
		if(location != -1)
			tempInhab = World.population[location];
		return tempInhab;
	}
	
	public double DistanceToFriend(Inhabitant friend)
	{
		double distance = -1;
		distance = Math.sqrt(Math.pow(Math.abs(x_pos - friend.getX()) , 2) + Math.pow(Math.abs(y_pos - friend.getY()) , 2)); 
		return distance;
	}
	
	public void Die()
	{
		isAlive = false;
	}
	
	
}
